<template>

  <div>
    <!-- 导航栏    placeholder  -->
<van-nav-bar  :fixed="true" left-arrow placeholder>
  <template #left>
    <img class="logo" src="../../assets/toutiao_logo.png">
  </template>
    <template #right>
    <van-icon name="search" size="0.48rem" @click="$router.push('/search')" />
  </template>
</van-nav-bar>
<!-- 频道 -->
<van-tabs v-model="active" animated sticky offset-top="1.16667rem">
  <van-tab :title="obj.name" v-for="obj in userChannelList" :key="obj.id" :name="obj.id"><ArticleList :channelId="active"></ArticleList></van-tab>

</van-tabs>
<!-- 频道的加号 -->
<van-icon @click="plusClickFn" name="plus" size="0.37333334rem" class="moreChannels"/>

  <!-- 底部反馈面板 -->
  <van-action-sheet
   v-model="$store.state.homeArticleActionShow"
   :actions="actionArr"
    @select="onSelect"
    @cancel="onCancel"
    @close="actionloseFn"
      :cancel-text="canceLText"
    get-container="body"
     />
     <!-- 加号的弹出层 -->
     <van-popup v-model="show" class="tan">
<ChannelEdit @closePE = "closeFn" :userChannelList="userChannelList"
@addChannel = 'addChannelFn'
@removeChannelEvent = 'removeChannelFn'
v-model="active"
>
</ChannelEdit>
     </van-popup>
  </div>
</template>

<script>

import { getUserChannelListAPI, disliskArticleAPI, reportArticleAPI, setUserChannelsAPI, removeChannelAPI } from '@/api'
import ArticleList from './ArticleList.vue'
import { mapState, mapMutations } from 'vuex'
import { Notify } from 'vant'
import ChannelEdit from './ChannelEdit.vue'

export default {
  data () {
    return {
      active: 0,
      userChannelList: [],
      show: false,
      actionArr: [],
      canceLText: '取消'

    }
  },
  async created () {
    const res = await getUserChannelListAPI()
    // console.log(res)
    this.userChannelList = res.data.data.channels
    // 默认反馈面板内容为一级
    this.actionArr = this.firstActions
  },
  components: {
    ArticleList,
    ChannelEdit
  },
  destroyed () {
    this.changeActionShowMutations(false)
  },
  computed: {
    ...mapState(['firstActions', 'secondActions', 'actionArtId'])

  },
  methods: {
    ...mapMutations(['changeActionShowMutations']),
    // 点击反馈面板选项
    async onSelect (item) {
      // 默认情况下点击选项时不会自动收起
      // 可以通过 close-on-click-action 属性开启自动收起
      if (item.name === '反馈垃圾内容') {
        this.actionArr = this.secondActions
        this.canceLText = '返回'
      } else if (item.name === '不感兴趣') {
        const res = await disliskArticleAPI(this.actionArtId)
        console.log(res)
        this.changeActionShowMutations(false)
        Notify({ type: 'success', message: '反馈成功,反正你也不知道' })
      } else {
        const res2 = await reportArticleAPI(
          {
            artId: this.actionArtId,
            type: item.id
          })
        console.log(res2)
        Notify({ type: 'success', message: '举报能咋地?' })
        setTimeout(() => {
          this.actionArr = this.firstActions
        }, 200)
        this.changeActionShowMutations(false)
      }
      // this.show = false
    },
    // 反馈面板关闭
    onCancel () {
      if (this.canceLText === '返回') {
        this.actionArr = this.firstActions
        this.canceLText = '取消'
        this.changeActionShowMutations(true)
      }
    },
    // +号频道编辑-点击事件
    plusClickFn () {
      this.show = true
    },
    // 编辑频道页面-关闭弹出层
    closeFn (bool) {
      this.show = bool
    },
    actionloseFn () {
      this.actionArr = this.firstActions
    },
    // 添加频道
    async  addChannelFn (channelObj) {
      this.userChannelList.push(channelObj)
      // console.log(this.userChannelList)
      const newArr = []

      this.userChannelList.forEach((obj, index) => {
        if (obj.id === 0) {
          // 推荐频道-不转换也不添加
        } else {
          const nweObj = { ...obj }
          newArr.seq = obj.index
          delete nweObj.name
          newArr.push(nweObj)
        }
      })
      await setUserChannelsAPI(newArr)
      // console.log(res)
    },
    // 删除频道
    async removeChannelFn (channelId) {
      const res = await removeChannelAPI(channelId)
      console.log(res)
      const index = this.userChannelList.findIndex(obj => obj.id === channelId)
      this.userChannelList.splice(index, 1)
    }

  }
}
</script>

<style lang="less"  scoped>
.logo {
  width: 100px;
  height: 30px;
}
// 设置 tabs 容器的样式
/deep/ .van-tabs__wrap {
  padding-right: 30px;
  background-color: #fff;
}

// 设置小图标的样式
.moreChannels {
  position: fixed;
  top: 62px;
  right: 8px;
  z-index: 999;
}
.tan,
.popup-container {
  background-color: transparent;
  height: 100%;
  width: 100%;
  background-color: white;
}

</style>
